package com.angel.shiro;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.pam.ModularRealmAuthenticator;
import org.apache.shiro.realm.Realm;

import java.util.Map;

public class SelfRealmAuthenticator extends ModularRealmAuthenticator {
    /**
     * 存放realm
     */
    private Map<String, Object> definedRealms;

    public void setDefinedRealms(Map<String, Object> definedRealms) {
        this.definedRealms = definedRealms;
    }

    @Override
    protected AuthenticationInfo doAuthenticate(AuthenticationToken authenticationToken) throws AuthenticationException {
        super.assertRealmsConfigured();
        Realm realm=null;
        UserNamePasswordToken token = (UserNamePasswordToken) authenticationToken;
        //判断用户类型
        if ("1".equals(token.getUserType())){
           realm = (Realm) this.definedRealms.get("userAuthorizingRealm");
        }else if("2".equals(token.getUserType())){
            realm = (Realm) this.definedRealms.get("systemAdminAuthorizingRealm");
        }
        return this.doSingleRealmAuthentication(realm,authenticationToken);
    }
}
